package com.ruoyi.carowner.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

import com.ruoyi.carowner.domain.CarownerKmLog;
import com.ruoyi.carowner.domain.CarownerPaymentRecords;
import com.ruoyi.carowner.domain.CarownerWallet;
import com.ruoyi.carowner.service.ICarownerKmLogService;
import com.ruoyi.carowner.service.ICarownerWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.carowner.mapper.CarownerKmMapper;
import com.ruoyi.carowner.domain.CarownerKm;
import com.ruoyi.carowner.service.ICarownerKmService;

/**
 * 里程Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-15
 */
@Service
public class CarownerKmServiceImpl implements ICarownerKmService {
    @Autowired
    private CarownerKmMapper carownerKmMapper;

    @Autowired
    private ICarownerKmLogService carownerKmLogService;

    @Autowired
    private ICarownerWalletService carownerWalletService;
    /**
     * 查询里程
     *
     * @param id 里程主键
     * @return 里程
     */
    @Override
    public CarownerKm selectCarownerKmById(Integer id) {
        return carownerKmMapper.selectCarownerKmById(id);
    }

    /**
     * 查询里程列表
     *
     * @param carownerKm 里程
     * @return 里程
     */
    @Override
    public List<CarownerKm> selectCarownerKmList(CarownerKm carownerKm) {
        return carownerKmMapper.selectCarownerKmList(carownerKm);
    }

    /**
     * 新增里程
     *
     * @param carownerKm 里程
     * @return 结果
     */
    @Override
    public int insertCarownerKm(CarownerKm carownerKm) {
        return carownerKmMapper.insertCarownerKm(carownerKm);
    }

    @Override
    public CarownerKm selectKmByUserid(Integer user_id) {
        return carownerKmMapper.selectKmByUserid(user_id);
    }

    /**
     * 修改里程
     *
     * @param carownerKm 里程
     * @return 结果
     */
    @Override
    public int updateCarownerKm(CarownerKm carownerKm) {
        LocalDate localDate = LocalDate.now();
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        Date date = Date.from(zonedDateTime.toInstant());
        if (carownerKm.getKm() == null || carownerKm.getUserId() == null) {
            return 0;
        }
        CarownerKmLog carownerKmLog = new CarownerKmLog();
        carownerKmLog.setKm(carownerKm.getUserParam());
        carownerKmLog.setExtime(date);
        carownerKmLog.setUserId(carownerKm.getUserId());
        carownerKmLogService.insertCarownerKmLog(carownerKmLog);


        Double userParam = carownerKm.getUserParam();
        userParam = userParam /= 10;

        CarownerWallet carownerWallet = carownerWalletService.selectCarownerWalletById(carownerKm.getId());
        carownerWallet.setBalance(carownerWallet.getBalance() - userParam);
        carownerWallet.setPaymoney(-userParam);
        carownerWallet.setPaytype(0);
        carownerWallet.setTransstatus(0);
        carownerWalletService.updateCarownerWallet(carownerWallet);

        System.out.println("11111111111");
        // 更新公里信息
        int rowsAffected = carownerKmMapper.updateCarownerKm(carownerKm);
        System.out.println("22222222222");
        if (rowsAffected == 0) {
            throw new RuntimeException("更新失败");
        }
        return rowsAffected;
    }

    /**
     * 批量删除里程
     *
     * @param ids 需要删除的里程主键
     * @return 结果
     */
    @Override
    public int deleteCarownerKmByIds(Integer[] ids) {
        return carownerKmMapper.deleteCarownerKmByIds(ids);
    }

    /**
     * 删除里程信息
     *
     * @param id 里程主键
     * @return 结果
     */
    @Override
    public int deleteCarownerKmById(Integer id) {
        return carownerKmMapper.deleteCarownerKmById(id);
    }
}
